//
//  ZZTaskVC.swift
//  QMXK
//
//  Created by jenkin on 2018/12/8.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZTaskVC: ZZBaseTableVC {
    
    var typeDict:Dictionary<String, Any>?
    lazy var saveClickBlock:ClickSaveBlock? = {
        weak var weakSelf = self
        let saveBlock:ClickSaveBlock = {(data:Dictionary?) in
            var data2:Dictionary? = data
            data2?["data"] = [:]
            var user:UserDefaults! = UserDefaults.standard
            ZZBaseUtil.shareUtil.saveTypeName(data: data2)
            self.getRecordData()
            self.tableView.reloadData()
        }
        return saveBlock
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setUI()
    }
    
    override func setUI() {
        super.setUI()
        self.myNavBar.isHidden = true
        self.tableView.frame = CGRect.init(x: 0, y: self.navH, width: self.screenW, height: self.screenH - self.bottomH - self.navH)
    
        self.tableView.register(UITableViewCell.classForCoder(), forCellReuseIdentifier: "Cell")
        let btn:UIButton = UIButton.init(frame: CGRect.init(x: self.screenW - 50, y: self.navH - 30, width: 30, height: 30))
        btn.setTitle("+", for: UIControl.State.normal)
        btn.setTitle("+", for: UIControl.State.selected)
        btn.setTitleColor(ColorConst.shareColorConst.titleColor(), for: UIControl.State.normal)
        btn.addTarget(self, action: #selector(addAction(sender:)), for: UIControl.Event.touchUpInside)
        self.view.addSubview(btn)

    }
    
    func getRecordData(){
        self.typeDict = ZZBaseUtil.shareUtil.getTypeName()
        self.data = Array(self.typeDict!.keys) as Array<AnyObject>
    }
    
    override init(tableStyle: UITableView.Style, tableFrame:CGRect){
        super.init(tableStyle: tableStyle, tableFrame: tableFrame)
        //self.tableFrame = CGRect.init(x: 0, y: self.navH, width: self.screenW, height: self.screenH - self.bottomH - self.navH)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
        let text:String = self.data[indexPath.row] as! String
//        let dataObject:AnyObject = self.data[indexPath.row]
//        let data = dataObject as! Dictionary<String, Any>
//        let text:String = data["name"] as! String
        cell.textLabel?.text = text;
        return cell;
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        var key:String = self.data[indexPath.row] as! String
        let taskDetailVC:ZZTaskDetailVC = ZZTaskDetailVC()
        taskDetailVC.dataDict = self.typeDict?[key] as! Dictionary<String, Any>
        self.navigationController?.pushViewController(taskDetailVC, animated: true)
    }
    
    @objc func addAction(sender: UIButton){
        let typeVC:ZZTaskTypeNameAddVC! = ZZTaskTypeNameAddVC.init()
        typeVC.clickSaveBlock = self.saveClickBlock
        self.navigationController?.pushViewController(typeVC, animated: true)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.getRecordData()
        self.tableView.reloadData()
    }
}
